public class Strings {
    public static String reverseString(String s) {
        StringBuffer str=new StringBuffer();
        int len=s.length();
        for(int i=0;i<len;i++){
            str.append(s.charAt(len-1-i));
        }
        return str.toString();
    }

    public static int firstUniqChar(String s) {
        int len=s.length();
        int chard[]=new int[256];
        for (int i=0;i<len;i++){
            chard[s.charAt(i)]++;
        }
        for (int i=0;i<len;i++){
            if(chard[s.charAt(i)]==1)return i;
        }
        return -1;
    }

    public static int strStr(String haystack, String needle) {
        int r=haystack.indexOf(needle);
        return r;
    }

    public static boolean isPalindrome(String s) {
        if(s==null||s.equals("")){
            return true;
        }
        int len=s.length();
        StringBuffer strb=new StringBuffer();
        for (int i=0;i<len;i++){
            char c=s.charAt(i);
            if(('a'<=c&&c<='z')||('A'<=c&&c<='Z')||('0'<=c&&c<='9')){
                strb.append(c);
            }
        }
        StringBuffer str2=strb;
        String str=strb.toString();
        str2.reverse();
        if(str2.toString().equalsIgnoreCase(str)){
            return true;
        }
        return false;
    }
    public static boolean isAnagram(String s, String t) {
        int chars[]=new int[256];
        int chart[]=new int[256];
        if(s.length()!=t.length()){
            return false;
        }
        int len=s.length();
        for (int i=0;i<len;i++){
            chars[s.charAt(i)]++;
            chart[t.charAt(i)]++;
        }
        for (int i=0;i<256;i++){
            if(chars[i]!=chart[i])return false;
        }
        return true;
    }

    public static String countAndSay(int n) {
        if(n<=0){
            return "error";
        }else if(n==1){
            return "1";
        }else {
            StringBuffer str=new StringBuffer(countAndSay(n-1));
            int len=str.length();
            StringBuffer result=new StringBuffer();
            for (int i=0;i<len;){
                int j;
                for(j=i+1;j<len;j++){
                    if(str.charAt(i)!=str.charAt(j)){
                        result.append(j-i).append(str.charAt(i));
                        i=j;
                        break;
                    }
                }
                if(j==len){
                    result.append(j-i).append(str.charAt(i));i=j;
                    break;
                }
            }
            return result.toString();
        }
    }

    public int myAtoi(String str) {
        int len=0;
        StringBuffer result;
        String r="";
        StringBuffer s=new StringBuffer(str);
        for (int i=0;i<s.length();i++){
            char c=s.charAt(i);
            if(c=='-'||('0'<=c&&c<='9')||c=='+'){
                result=new StringBuffer();
                result.append(c);
                int j;
                for (j=i+1;j<s.length();j++){
                    c=s.charAt(j);
                    if('0'<=c&&c<='9'){
                        result.append(c);
                    }else {
                        break;
                    }
                }
                    len = j - i;
                    r = result.toString();
                break;
            }else if(('a'<=c&&c<='z')||('A'<=c&&c<='Z')){
                break;
            }
        }
        if(len==0)return 0;
        else {
            if(r.equals("-")||r.equals("+"))return 0;
            try{
                return  Integer.valueOf(r);
            }catch (Exception e){
                if(r.charAt(0)=='-')return Integer.MIN_VALUE;
                else return Integer.MAX_VALUE;
            }
        }
    }

    public String longestCommonPrefix(String[] strs) {
        int len=strs.length;
        try{
            String temp=strs[0];
            int min=strs[0].length();
            for(int i=1;i<len;i++){
                if(strs[i].length()<min){min=strs[i].length();temp=strs[i];}
            }
            int flag;
            while (temp.length()>0){
                flag=0;
                for (int i=0;i<len;i++){
                    if(strs[i].indexOf(temp)==0){
                        flag++;
                    }else {
                        break;
                    }
                }
                if(flag==len)break;
                temp=temp.substring(0,temp.length()-1);
            }
            return temp;
        }catch(Exception e){
            return "";
        }
    }
}
